<?php

class DefaultController extends Controller
{
	public $layout = 'awt-reference';
	
	public function actionIndex()
	{
		#status of a ref
		//$rs = ReferenceInterface::status(5);
		//echo "<pre>"; var_dump( $rs ); exit;
		
		#borrow book
		//ReferenceInterface::create(5,Yii::app()->user->id);
		
		#return book
		//ReferenceInterface::update(2,Yii::app()->user->id);
		
		#my borrowed
		//$rs = ReferenceInterface::query(array(
		//	'user_id'=>Yii::app()->user->id,
		//));
		
		#my borrowed and not return
		//$rs = ReferenceInterface::query(array(
		//	'user_id'=>Yii::app()->user->id,
		//	'date_update'=>null,
		//));
		
		$this->render('index');
	}
	
	public function actionSearch() {
		
		$referenceSearcher = new ReferenceSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($referenceSearcher);
		$searcher->normalSearch();

		$this->render('search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));
	}
	
	public function actionQuickview($id) {
		
		$this->layout = 'awt-quickview';
		
		$status = ReferenceInterface::status($id,true);
		$model = Reference::model()->with('album')->findByPk($id);
		
		$this->render('quickview',array(
			'rs'=>$model,
			'status'=>$status,
		));
	}
	
	public function actionCreate($id=null) {
		
		$model = new Reference();
		
		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			
			$id = $_POST['Reference']['id'];
			$reference = $model->findByAttributes(array('id'=>$id));
			
			if ($reference) {
				
				$rs = ReferenceInterface::create($reference->id,Yii::app()->user->id);
				
				if ($rs) {
					Yii::app()->user->setFlash('success',"You have borrow WM".str_pad($id,3,'0',STR_PAD_LEFT));
					$this->redirect(array('history'));
				} else {
					Yii::app()->user->setFlash('error','WM'.str_pad($id,3,'0',STR_PAD_LEFT)." is not available for borrowed");
				}
			} else {
				$code = 'WM'.str_pad($rs->id,3,'0',STR_PAD_LEFT);
				Yii::app()->user->setFlash('error',"$code Not found");
			}
			
			$this->refresh();
		}
		
		if ($id!==null) $model->id = str_pad($id,3,'0',STR_PAD_LEFT);
		$this->render('create',array(
			'model'=>$model,
		));
	}
	public function actionUpdate($id=null) {
		
		$model = new Reference();
		
		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			
			$id = $_POST['Reference']['id'];
			$reference = $model->findByAttributes(array('id'=>$id));
			
			if ($reference) {
				
				$rs = ReferenceInterface::update($reference->id,Yii::app()->user->id);
				
				if ($rs) {
					Yii::app()->user->setFlash('success',"You have return WM".str_pad($id,3,'0',STR_PAD_LEFT));
					$this->redirect(array('default/history'));
				} else {
					Yii::app()->user->setFlash('error','WM'.str_pad($id,3,'0',STR_PAD_LEFT)." is not borrowed");
				}
			} else {
				$code = 'WM'.str_pad($rs->id,3,'0',STR_PAD_LEFT);
				Yii::app()->user->setFlash('error',"$code Not found");
			}
			
			$this->refresh();
		}
		
		if ($id!==null) $model->id = str_pad($id,3,'0',STR_PAD_LEFT);
		$this->render('update',array(
			'model'=>$model,
		));
	}
	
	public function actionHistory() {
		
		$model = new Reference();
		$rs = ReferenceInterface::query(array(
			'user_id'=>Yii::app()->user->id,
		));
		
		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			$reference = $model->findByAttributes(array('code'=>$code));
			if ($reference) {
				
				$rs = ReferenceInterface::update($reference->id,Yii::app()->user->id);
				if ($rs) {
					Yii::app()->user->setFlash('success',"You have return a book");
					$this->redirect('reference/default/history');
				} else {
					Yii::app()->user->setFlash('error',"This Book is not borrowed");
				}
			}
		}
		$this->render('history',array(
			'model'	=>$model,
			'rs'	=>$rs,
		));
		
	}
}